﻿/*******************************************************
** dndListener Class
** Written by JabbyPanda, JudahPanda
* 
*  Inspired from DDTrash class written by Jonathan Doklovic
** 2003 Fuseware LTD.
**
** This is the listener class. A movieclip that inherits this class can 
** receive onDragOver, onDragOut and onDrop events from any other dndUIWidget
* 
*******************************************************/
import mx.events.EventDispatcher;
import mx.utils.Delegate;
import com.troyworks.ui.managers.DndUIWidgets;

//register our class
class com.troyworks.ui.managers.DndListener extends MovieClip {
	private var _inited:Boolean;
	
	public var addEventListener:Function;
	public var removeEventListener:Function;
	private var dispatchEvent:Function;
	private var isOver:Boolean;
	public var noDropIcon:MovieClip;

	function DndListener() {
		if (!_inited) init();
	}
	
	// priv methods
	private function init():Void{
		EventDispatcher.initialize(this);
		_inited = true;
		var crossdndWidgetList:DndUIWidgets = DndUIWidgets.getWidgetList ();	
		crossdndWidgetList.addItem(this);
	}
	
	// *******************
	// ** onDraggerMove **
	// *******************
	// ******************************************************
	// ** This method captures movement of something being
	// ** dragged.
	// *****************************************************
	function onDraggerMove(eventObj:Object):Void {
		//trace("drag move")
		// if node is on the trash, show the on state
		if (this.hitTest(_root._xmouse, _root._ymouse)) {
			if (!isOver) {
				//trace("drag hit move")
				hideCustomCursor();
				dispatchEvent ({type:"onDragOver", target:this, drag_mc:eventObj.target});
				isOver = true;
			}
		} else {
			if(isOver) {
				showCustomCursor();
				dispatchEvent ({type:"onDragOut", target:this, drag_mc:eventObj.target});	
				isOver = false;
			}
		}
	}
	
	// *******************
	// ** onDraggerDrop **
	// *******************
	// ******************************************************
	// ** This method captures the drop event when the drag_mc is
	// ** dropped.
	// *****************************************************
	function onDraggerDrop(eventObj:Object):Void  {
		
		// if dragger is dropped on the trash, run the onDrop function
		if (this.hitTest(_root._xmouse, _root._ymouse)) {
			hideCustomCursor();
			dispatchEvent ({type:"onDrop", target:this, drag_mc:eventObj.target});
		}
	}

	// shows the custom "noDrop possible" cursor
	public function showCustomCursor():Void {
		Mouse.hide();
		noDropIcon._visible = true;		
	}
	
	
	// hides the custom "noDrop possible" cursor
	public function hideCustomCursor():Void 	{
		Mouse.show();
		noDropIcon._visible = false;
	}
}
